// pages/Complaint/complaintList/complaintList.js
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    object:'',
    time:'',
    content:'',
    uid:'',
    pid:'',
    myComplaints:[],
  },
  //确定投诉
  onSubmit(){
    wx.request({
      url: 'http://8.129.104.110/complaint',
      data: {
        complaintTime: this.time,
        deleted: false,
        complaintContent: this.content,
        propertyId: this.pid,
        userId: this.uid
      },
      method:'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        //更新已有投诉列表
        this.getAllComplaint()
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取所有投诉
    this.getAllComplaint()
  },
  //获取所有投诉
  getAllComplaint(){
    var that=this
    wx.request({
      url: 'http://8.129.104.110/complaint/1/999',
      data: {
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        // console.log(res.data.data)
        var data=res.data.data
        that.setData({
          myComplaints:data.records
        })
      }
    })
  },
  //确定删除
  onClose(event) {
    const { position, instance, name } = event.detail;
    // console.log(event.detail)
    switch (position) {
      case 'left':
      case 'cell':
        instance.close();
        break;
      case 'right':
        Dialog.confirm({
          message: '确定删除吗？',
        }).then(() => {
          instance.close();
          wx.request({
            url: 'http://8.129.104.110/complaint/'+name,
            header: {
              'content-type': 'application/json' // 默认值
            },
            method:'delete',
            success (res) {
              //更新已有投诉列表
              this.getAllComplaint()  
            }
          })
        })
        break;
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})